﻿Public Class AGENTOFMAKER
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub filterDrugMaker()
        Dim s5 As FILTERCLASS
        Dim sql5 As String = "SELECT CAST(DRGMID AS CHAR(8)) AS DRGMID, MAKERNAME, REMARK FROM masdrgmaker"
        s5 = New FILTERCLASS(drugMakerTxt, sql5, "รหัสผู้ผลิตยา,ชื่อผู้ผลิตยา,หมายเหตุ", "80,200,200", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s5.SetShowBorder = True
    End Sub

    Private Sub filterDrugagent()
        Dim s5 As FILTERCLASS
        Dim sql5 As String = "SELECT CAST(AGENTCODE AS CHAR(4)) AS AGENTCODE, COMPANY_NAME FROM agent WHERE AGENTCODE NOT IN (SELECT AGENTCODE FROM makeragent WHERE DRGMID = '" & drugMakerTxt.Tag & "');"
        s5 = New FILTERCLASS(drugAgentTxt, sql5, "รหัสผู้จำหน่ายเวชภัณฑ์,ชื่อผู้จำหน่ายเวชภัณฑ์", "80,200", "1,1", "1,1")
        's.SetShowInDown = False
        s5.SetShowBorder = True
    End Sub

    Private Sub drugMakerTxt_Click(sender As Object, e As EventArgs) Handles drugMakerTxt.Click
        drugMakerTxt.Clear()
    End Sub

    Private Sub drugAgentTxt_Click(sender As Object, e As EventArgs) Handles drugAgentTxt.Click
        drugAgentTxt.Clear()
    End Sub

    Private Sub drugMakerTxt_TextChanged(sender As Object, e As EventArgs) Handles drugMakerTxt.TextChanged
        filterDrugagent()
    End Sub

    Private Sub queryMakeragentGrid()
        Dim objSelectMakeragent As New DRUGAGENTOFDRUGMAKERCLASS
        Dim dt As DataTable = connect.GetTable(objSelectMakeragent.selectMakeragent_)
        makeragentGrid.PrimaryGrid.DataSource = dt
    End Sub

    Private Sub DRUGAGENTOFDRUGMAKER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        filterDrugMaker()
        filterDrugagent()
        queryMakeragentGrid()

        DesignForm(Me)
        DesignGrid(makeragentGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub clearForm()
        MKAGIDTxt.Clear()
        drugMakerTxt.Clear()
        drugAgentTxt.Clear()
        makerAgentStatusChk.Checked = False
    End Sub

    Private Sub makeragentGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles makeragentGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        Try
            MKAGIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ID").Value()
        Catch ex As Exception
            MKAGIDTxt.Clear()
        End Try

        Try
            drugMakerTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ผู้ผลิต").Value()
            drugMakerTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผู้ผลิต").Value()
        Catch ex As Exception
            drugMakerTxt.Clear()
        End Try

        Try
            drugAgentTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตัวแทนจำหน่าย").Value()
            drugAgentTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสตัวแทน").Value()
        Catch ex As Exception
            drugAgentTxt.Clear()
        End Try

        makerAgentStatusChk.Checked = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("makeragent", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "status", "mkagid", MKAGIDTxt.Text)

        filterDrugagent()
    End Sub

    Private Sub addDaOfDm_Click(sender As Object, e As EventArgs) Handles addDaOfDm.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If drugMakerTxt.Text.Length.ToString > 0 And drugAgentTxt.Text.Length.ToString > 0 Then
                    Dim objInsertData As New DRUGAGENTOFDRUGMAKERCLASS
                    objInsertData.DRGMID_ = drugMakerTxt.Tag
                    objInsertData.AGENTCODE_ = drugAgentTxt.Tag
                    objInsertData.STATUS_ = makerAgentStatusChk.Checked

                    If objInsertData.insertMakeragent() = True Then
                        MsgBox("ท่านได้ทำการเพิ่มตัวแทนของผู้ผลิตยาเรียบร้อยแล้ว")
                        queryMakeragentGrid()
                        clearForm()
                    End If

                    connect.Dispose()
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editDaOfDm_Click(sender As Object, e As EventArgs) Handles editDaOfDm.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If MKAGIDTxt.Text.Length.ToString > 0 And drugMakerTxt.Text.Length.ToString > 0 And drugAgentTxt.Text.Length.ToString > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขการจับคู่ตัวแทนจำหน่านยและผู้ผลิตดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New DRUGAGENTOFDRUGMAKERCLASS
                        objEditData.MKAGID_ = MKAGIDTxt.Text
                        objEditData.DRGMID_ = drugMakerTxt.Tag
                        objEditData.AGENTCODE_ = drugAgentTxt.Tag
                        objEditData.STATUS_ = makerAgentStatusChk.Checked

                        If objEditData.editMakeragent() = True Then
                            MsgBox("ท่านได้ทำการแก้ไขตัวแทนของผู้ผลิตยาเรียบร้อยแล้ว")
                            queryMakeragentGrid()
                            clearForm()
                        End If

                        connect.Dispose()
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("makeragent")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("makeragent")
        nextform.ShowDialog()
    End Sub
End Class